<template>
    <div>

        <div v-show="ad==1">
            <el-menu>
                <el-menu-item><a @click="GetExpType"  >+ 添加</a></el-menu-item>
        </el-menu>


            <el-table :data="workhistorys" height="200" border style="width: 100%">
                <el-table-column label="编号" prop="id"></el-table-column>
                <el-table-column label="起止时间" prop="workhistoryStartdate"></el-table-column>
                <el-table-column label="结束时间" prop="workhistoryEnddate"></el-table-column>
                <el-table-column label="工作单位" prop="workhistoryWorkunit"></el-table-column>
                <el-table-column label="工作职务 " prop="workhistoryJobPosition"></el-table-column>
                <el-table-column label="工作类别" prop="workhistoryWorktype"></el-table-column>
                <el-table-column label="单位性质" prop="workhistoryWorknature"></el-table-column>
                <el-table-column label="所在地" prop="workhistorySite"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown>
                            <el-button type="primary" size="small">
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item size="small" @click.native="UpExpType(scope)">修改</el-dropdown-item>
                                <el-dropdown-item size="small" @click.native="del(scope)">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="ad==2">
            <el-form :model="worke" :rules="rules" ref="recordForm" class="demo-ruleForm">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="起止时间：" prop="workhistoryStartdate">
                            <el-input v-model="worke.workhistoryStartdate" type="date" size="small" placeholder="起止时间"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="结束时间：" prop="workhistoryEnddate">
                            <el-input v-model="worke.workhistoryEnddate" type="date" size="small" placeholder="结束时间"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="工作单位：" prop="workhistoryWorkunit">
                            <el-input v-model="worke.workhistoryWorkunit" size="small" placeholder="工作单位"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="工作职务：" prop="workhistoryJobPosition">
                            <el-input v-model="worke.workhistoryJobPosition" size="small" placeholder="工作职务"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="工作类别：" prop="workhistoryWorktype">
                            <el-input v-model="worke.workhistoryWorktype" size="small" placeholder="工作类别"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="单位性质：" >
                            <el-input v-model="worke.workhistoryWorknature" size="small" placeholder="单位性质"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="所在地：">
                            <el-input v-model="worke.workhistorySite"  size="small" placeholder="所在地"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item >
                    <el-button type="primary" @click="addexptype('recordForm')" :disabled="disable" size="small" >提交</el-button>
                    <el-button type="primary" @click="coles" size="small" >取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import hrworkhi from "../../../model/gsl/HrWorkhistory/hrworkhistory";
    const hrw=hrworkhi.getInstance();
    export default {
        props:["eorkId"],
        name: "ExpressType",
        data(){
            var workhistoryStartdate = (rule, value, callback) => {
                value=this.worke.workhistoryStartdate;
                if (!value) {
                    this.disable=true;
                    callback(new Error('请选择起止日期'));
                } else {
                    callback();
                    this.disable=false;
                }
            }; var workhistoryEnddate = (rule, value, callback) => {
                value=this.worke.workhistoryEnddate;
                if (!value) {
                    this.disable=true;
                    callback(new Error('请选择结束日期'));
                } else {
                    callback();
                    this.disable=false;
                }
            }; var workhistoryWorkunit = (rule, value, callback) => {
                value=this.worke.workhistoryWorkunit;
                if (!value) {
                    this.disable=true;
                    callback(new Error('请输入工作单位'));
                } else {
                    callback();
                    this.disable=false;
                }
            }; var workhistoryJobPosition = (rule, value, callback) => {
                value=this.worke.workhistoryJobPosition;
                if (!value) {
                    this.disable=true;
                    callback(new Error('请输入职务'));
                } else {
                    callback();
                    this.disable=false;
                }
            }; var workhistoryWorktype = (rule, value, callback) => {
                value=this.worke.workhistoryWorktype;
                if (!value) {
                    this.disable=true;
                    callback(new Error('请输入性质'));
                } else {
                    callback();
                    this.disable=false;
                }
            };
            return{
                workhistorys:[],
                worke:{},
                empId: JSON.parse(JSON.stringify(this.eorkId)),//父类传来的id
                ad:1,
                disable:true,
                rules: {
                    workhistoryStartdate: [{ required: true,validator: workhistoryStartdate, trigger: 'blur' }],
                    workhistoryEnddate: [{ required: true,validator: workhistoryEnddate, trigger: 'blur' }],
                    workhistoryWorkunit: [{ required: true,validator: workhistoryWorkunit, trigger: 'blur' }],
                    workhistoryJobPosition: [{ required: true,validator: workhistoryJobPosition, trigger: 'blur' }],
                    workhistoryWorktype: [{ required: true,validator: workhistoryWorktype, trigger: 'blur' }],
                }
            }
        },
        created(){
            this.getall();
        },
        methods: {
            getall(){
                hrw.listByempId(this.empId).then((re)=>{
                    this.workhistorys=re.data.data;
                })
            },
            del(so){
                hrw.remove(so.row.id).then((re)=>{
                    if (re.data.data==1) {
                        this.$message.success({
                            message:'删除成功',
                            duration:1000
                        })
                        this.getall();
                    }else {
                        alert("删除失败！")
                        this.getall();
                    }
                })
            },
            GetExpType(){
                this.worke={}
                this.ad=2;
            },
            UpExpType(ss){
                this.ad=2;
                hrw.listByid(ss.row.id).then((re)=>{
                    this.worke={}
                    this.worke=re.data.data;
                })
            },
            coles(){
                this.ad=1;
            },
            addexptype(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.worke.hrEmpId=this.empId;
                        if (this.worke.id) {
                            hrw.update(this.worke).then((re)=>{
                                if (re.data.data==1) {
                                    this.$message.success({
                                        message:'修改成功',
                                        duration:1000
                                    })
                                    this.getall();
                                    this.worke={}
                                    this.ad=1;
                                }else {
                                    alert("修改失败！")
                                    this.ad=2;
                                }
                            })
                        }else {
                            hrw.save(this.worke).then((re)=>{
                                if (re.data.data==1) {
                                    this.$message.success({
                                        message:'添加成功',
                                        duration:1000
                                    })
                                    this.worke={}
                                    this.getall();
                                    this.ad=1;
                                }else {
                                    alert("添加失败！")
                                    this.ad=2;
                                }
                            })
                        }
                        this.worke={};
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        }
    }
</script>

<style scoped>

</style>